//
//  LoginContairView.m
//  Yimei
//
//  Created by xiaowu xiaowu on 2017/11/8.
//  Copyright © 2017年 xiaowu xiaowu. All rights reserved.
//

#import "LoginContairView.h"
@interface LoginContairView ()<UITextFieldDelegate>
@property (nonatomic,strong)UITextField *nameTextField;
@property (nonatomic,strong)UITextField *telTextField;
@property (nonatomic,strong)UITextField *codeTextField;
@property (nonatomic,strong)UITextField *pswTextField;

@property (nonatomic,strong)UIButton *forgetButton;
@property (nonatomic,strong)UIButton *regButton;
@end
@implementation LoginContairView

+(LoginContairView *)getLoginContairView:(UIView *)view{
    LoginContairView*contairView = [[LoginContairView alloc]init];
    [view addSubview:contairView];
    [contairView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(view);
        make.centerY.mas_equalTo(view);
        make.width.mas_equalTo(view).multipliedBy(0.77);
        make.height.mas_equalTo(310.0);
    }];
    return contairView;
}
-(instancetype)init{
    if(self = [super init]){
        [self setup];
    }
    return self;
}

-(void)viewWillAppear{
    
    if([LoginModel shareLoginModel].mobile.length > 0){
        [self.telTextField setText:[LoginModel shareLoginModel].mobile];
        self.viewModel.phone = self.telTextField.text;
    }
    
}

-(void)setup{
    
    
    _viewModel = [TPLoginViewModel new];
    
    
    self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.2];
    ViewRadius(self, 3.0);
    _nameTextField = [self setup_placeholder:@"请输入姓名" leftImage:@"TP_login_name_icon"];
    _telTextField = [self setup_placeholder:@"请输入手机号" leftImage:@"TP_tel_textField_image"];
    _codeTextField = [self setup_placeholder:@"请输入验证码" leftImage:@"TP_code_textField_image"];
    _pswTextField = [self setup_placeholder:@"请输入密码" leftImage:@"TP_Password_textField_image"];
    _pswTextField.secureTextEntry = YES;
    
     RAC(_viewModel,psw) = _pswTextField.rac_textSignal;
     RAC(_viewModel,phone) = _telTextField.rac_textSignal;
     RAC(_viewModel,name) = _nameTextField.rac_textSignal;
     RAC(_viewModel,code) = _codeTextField.rac_textSignal;
    
    
    
    [self.titleButton setBackgroundImageColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.titleButton addTouchUpInsideTarget:self action:@selector(actionClick:)];
    
    ViewRadius(self.titleButton, 4.0);
    
    
   UIButton *codeButton = [_codeTextField xw_addRightCodeViewFrame:CGRectMake(0, 0, 85, 38.0) string:@"获取验证码" btnFrame:CGRectMake(0,5,90.0,28) color:[UIColor whiteColor]];
    codeButton.tag = LoginContairViewStyleCode;
    [codeButton addTouchUpInsideTarget:self action:@selector(actionClick:)];
   
    self.titleButton.tag = LoginContairViewStyleTelLogin;
    self.titleButton.titleLabel.font = fontSystemOfSize(16);
    [self.titleButton setTitleColor:TPAllBackgroundColor forState:UIControlStateNormal];
    [self.titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.bottom.mas_equalTo(-20.0);
        make.width.mas_equalTo(self).multipliedBy(0.5);
        make.height.mas_equalTo(34.0);
    }];
    
    
    
    self.forgetButton = [UIButton buttonWithFrame:CGRectZero title:@"忘记密码?" font:fontSystemOfSize(13) color:[UIColor whiteColor]];
    [self.forgetButton addTouchUpInsideTarget:self action:@selector(actionClick:)];
    self.forgetButton.tag = LoginContairViewStyleFinePass;
    
    self.regButton = [UIButton buttonWithFrame:CGRectZero title:@"没有账号，去注册>>" font:fontSystemOfSize(13) color:[UIColor whiteColor]];
    [self.regButton addTouchUpInsideTarget:self action:@selector(actionClick:)];
    self.regButton.titleLabel.textAlignment = NSTextAlignmentRight;
    self.regButton.tag = LoginContairViewStyleSinin;
    
    
}
-(void)actionClick:(UIButton *)sender{
    
   if(self.block)
       self.block(sender.tag, sender);
}
-(void)setStyle:(LoginContairViewStyle)style{
    _style = style;
    
    self.nameTextField.hidden = YES;
    self.codeTextField.hidden = YES;
    self.pswTextField.hidden = YES;
    
    self.forgetButton.hidden = YES;
    self.regButton.hidden = YES;
    
    CGFloat offset = 10.0;
    CGFloat height = 38.0f;
    switch (style) {
        case LoginContairViewStyleTelLogin: //快捷手机
            {
             
                [self.titleButton setTitle:@"登录" forState:0];
                self.codeTextField.hidden = NO;
                [self.telTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.mas_equalTo(self);
                    make.width.mas_equalTo(self).multipliedBy(0.8);
                    make.height.mas_equalTo(height);
                    make.centerY.mas_equalTo(self).offset(-35.0);
                }];
                [self.codeTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.mas_equalTo(self);
                    make.width.mas_equalTo(self.telTextField.mas_width);
                    make.height.mas_equalTo(self.telTextField.mas_height);
                    make.top.mas_equalTo(self.telTextField.mas_bottom).offset(offset);
                }];
                
                self.forgetButton.hidden = self.regButton.hidden = NO;
                
                [self.pswTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.mas_equalTo(self);
                    make.width.mas_equalTo(self.telTextField.mas_width);
                    make.height.mas_equalTo(self.telTextField.mas_height);
                    make.top.mas_equalTo(self.telTextField.mas_bottom).offset(offset);
                }];
                
            }
            break;
        case LoginContairViewStyleTelPsw:
        {
            [self.titleButton setTitle:@"登录" forState:0];
            self.pswTextField.hidden = NO;
            [self.telTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(self);
                make.width.mas_equalTo(self).multipliedBy(0.8);
                make.height.mas_equalTo(height);
                make.centerY.mas_equalTo(self).offset(-35.0);
            }];
            [self.pswTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(self);
                make.width.mas_equalTo(self.telTextField.mas_width);
                make.height.mas_equalTo(self.telTextField.mas_height);
                make.top.mas_equalTo(self.telTextField.mas_bottom).offset(offset);
            }];
            
             self.forgetButton.hidden = self.regButton.hidden = NO;
        }
            break;
        case LoginContairViewStyleSinin://注册
        {
            self.regButton.hidden = NO;
            [self.regButton setTitle:@"已有账号，去登录>>" forState:0];
            [self.titleButton setTitle:@"注册" forState:0];
            self.nameTextField.hidden = NO;
            self.codeTextField.hidden = NO;
            self.pswTextField.hidden = NO;
            [self.nameTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(self);
                make.width.mas_equalTo(self).multipliedBy(0.8);
                make.height.mas_equalTo(height);
                make.top.mas_equalTo(50.0);
            }];
            [self.telTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(self);
                make.width.mas_equalTo(self).multipliedBy(0.8);
                make.height.mas_equalTo(height);
                make.top.mas_equalTo(self.nameTextField.mas_bottom).offset(offset);
            }];
            [self.codeTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(self);
                make.width.mas_equalTo(self).multipliedBy(0.8);
                make.height.mas_equalTo(height);
                make.top.mas_equalTo(self.telTextField.mas_bottom).offset(offset);
            }];
            [self.pswTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(self);
                make.width.mas_equalTo(self).multipliedBy(0.8);
                make.height.mas_equalTo(height);
                make.top.mas_equalTo(self.codeTextField.mas_bottom).offset(offset);
            }];
            
            
            RAC(self.titleButton,enabled) = [self.viewModel validSigneSignal];
        }
            break;
            
        case LoginContairViewStyleFinePass:{
            
            [self.titleButton setTitle:@"完成" forState:0];
            
            self.codeTextField.hidden = NO;
            self.pswTextField.hidden = NO;
           
            [self.codeTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(self);
                make.width.mas_equalTo(self).multipliedBy(0.8);
                make.height.mas_equalTo(height);
                make.centerY.mas_equalTo(self);
            }];
            [self.telTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(self);
                make.width.mas_equalTo(self).multipliedBy(0.8);
                make.height.mas_equalTo(height);
                make.bottom.mas_equalTo(self.codeTextField.mas_top).offset(-offset);
            }];
           
            [self.pswTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(self);
                make.width.mas_equalTo(self).multipliedBy(0.8);
                make.height.mas_equalTo(height);
                make.top.mas_equalTo(self.codeTextField.mas_bottom).offset(offset);
            }];
        }
            
        default:
            break;
    }
    
    [self layoutIfNeeded];
    
}
-(void)setLoginStyle:(LoginContairViewStyle)loginStyle{
    _loginStyle = loginStyle;
    
    self.style = loginStyle;
    switch (loginStyle) {
        case LoginContairViewStyleTelLogin:
        {
            self.pswTextField.hidden = YES;
            self.codeTextField.hidden = NO;
            
//            RAC(self.titleButton,enabled) = [self.viewModel validLoginCodeSignal];
        }
            break;
        case LoginContairViewStyleTelPsw:
        {
            self.pswTextField.hidden = NO;
            self.codeTextField.hidden = YES;
//            RAC(self.titleButton,enabled) = nil;
//            RAC(self.titleButton,enabled) = [self.viewModel validLoginSignal];
        }
            break;
            
        default:
            break;
    }
}
-(UITextField *)setup_placeholder:(NSString *)placeholder leftImage:(NSString *)image{
    UITextField*textFiled = [UITextField initWithFrame:CGRectZero borderStyle:UITextBorderStyleNone fontSize:15 placeholder:placeholder text:@""];
    textFiled.delegate = self;
    
    [textFiled xw_addLeftImageViewFrame:CGRectMake(0, 0, 40.0, 40.0) image: ImageNamed(image) imageFrame:CGRectMake(10, 10, 20.0, 20.0)];
        [textFiled setPlaceholderLabelTextColor:[UIColor whiteColor]];
    textFiled.textColor = [UIColor whiteColor];
    //    textFiled.secureTextEntry = YES;
    textFiled.delegate = self;
    
    [textFiled tp_addLineBottomWithColor:[UIColor whiteColor]];
    [self addSubview:textFiled];
    return textFiled;
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
-(void)willMoveToSuperview:(UIView *)newSuperview{
    
    [super willMoveToSuperview:newSuperview];
}
-(void)layoutSubviews{
    [super layoutSubviews];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:ImageNamed(@"TP_login_layer_header_icon")];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.superview addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80.0, 80.0));
        make.bottom.mas_equalTo(self.mas_top).offset(40.0);
        make.centerX.mas_equalTo(self);
    }];
    
    UIImageView *layerImageView = [[UIImageView alloc]initWithImage:ImageNamed(@"TP_login_layer_icon")];
    layerImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.superview addSubview:layerImageView];
    [layerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(115, 105));
        make.bottom.mas_equalTo(self.mas_top).offset(52.0);
        make.centerX.mas_equalTo(self);
    }];
    
    
     [self.superview addSubview:self.forgetButton];
     [self.superview addSubview:self.regButton];
    
    [self.forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left);
        make.top.mas_equalTo(self.mas_bottom);
        make.height.mas_equalTo(44.0);
    }];
    
    [self.regButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right);
        make.top.mas_equalTo(self.mas_bottom);
        make.height.mas_equalTo(44.0);
    }];
    
}

@end
